package com.springcloud.sc.ucpv5.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.springcloud.sc.ucpv5.client.module.map.data.MapHistoryFilter;
import com.springcloud.sc.ucpv5.client.module.map.data.MapTileLoadData;
import com.springcloud.sc.ucpv5.client.module.map.util.MapTileUtil;
import com.springcloud.sc.ucpv5.util.DateTool;
import com.springcloud.sc.ucpv5.util.OperationResult;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/map")
public class UcpMapController {
    /**
     * 地图瓦片等数据获取接口
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMapResApi", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public OperationResult getMapResApi( @RequestBody JSONObject jsonParam ){
        int operation = jsonParam.getIntValue("operation"); // 1 下载 tile 2 获取地图位置描述
        String paras = jsonParam.getString("paras");

        OperationResult result_info = new OperationResult();
        if (operation == 1) {
            // 获取Tile
            MapTileLoadData tileInfo = (MapTileLoadData) JSON.parseObject(paras, MapTileLoadData.class);
            byte[] data = MapTileUtil.getTile(tileInfo);

            if (data != null) {
//                result_info.resultContent = ImgTransHelper.encode(data);
            } else {
                result_info.errorCode = -1;
                result_info.errorDesc = "无图片";
            }
        }
        return result_info;
    }

    /**
     * 地图历史轨迹获取接口
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMapHistoryQueryApi", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public OperationResult getMapHistoryQueryApi( @RequestBody JSONObject jsonParam ){
        System.out.println("收到getMapHistoryQueryApi:" + jsonParam.toJSONString());
        OperationResult result_info = new OperationResult();
        try {
            MapHistoryFilter mapHistoryDataFilter = JSON.parseObject(jsonParam.toJSONString(), MapHistoryFilter.class);

            Date beginDate = DateTool.getDateFromString(mapHistoryDataFilter.time_begin);

            Date endDate = DateTool.getDateFromString(mapHistoryDataFilter.time_end);
            if (beginDate == null || endDate == null) {
                result_info.errorCode = 1;
                result_info.errorDesc = "查询条件有误, 请确认条件时间格式为 yyyy-MM-dd HH:mm:ss";
            } else {

            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result_info;
    }

}
